/*
 * Copyright © 2011 by PropSage Pte Ltd. 
 * All Rights Reserved.
 * licensing@propsage.com
 * http://www.propsage.com/terms.html
 * 
 * 
 * Javascript for /case/index
 * 
 * @author Poh Nean
 */
propsage.namespace("propsage.brochure");


propsage.brochure.index = (function($, ui){
	var cursorPos;
	var selectedTextBox;
	var selectedTextBoxCurrentHeight;
	var shiftUps = {
		192: "~",
		49: "!",
		50: "@",
		51: "#",
		52: "$",
		53: "%",
		54: "^",
		55: "&",
		56: "*",
		57: "(",
		48: ")",
		173: "_",
		61: "+",
		219: "{",
		221: "}",
		220: "|",
		59: ":",
		222: "\"", 
		188 :"<", 
		190 :">", 
		191 :"?"
	};

	
	
	function onColumnKeydown(e) {
		var self = this;
		setTimeout(function() {
			console.log(e);
		}, 5);
	}
	function onColumnClick(e) {
		var self = this;
		selectedTextBox = this;
		selectedTextBoxCurrentHeight = $(this).closest('.column').height();
		
		defineCheckBoxCSS($(this));
	}	
	
	function defineCheckBoxCSS(master){
		$("#checkHeight").copyCSS(master);
		$("#checkHeight").css("height","auto");
		currentHeight = visualHeight($(selectedTextBox), "");
	}
	
	
	function visualHeight($el,$newChar) {
		$('#checkHeight').html($el.html() + $newChar);
		
		return($('#checkHeight').outerHeight());
	}
	
	function removeLastChar(nodes) {
		var lastNode, nodeType;
		var length = nodes.length;
		
		if (length > 1) {
			lastNode = nodes[length - 1];
		
			nodeType = lastNode.nodeType;
			
			if (nodeType == 1) {
				removeLastChar(lastNode.childNodes);
			} else {
				removeLastCharInTextNode(lastNode);
			}
		} else {
			lastNode = nodes[0];
			
			removeLastCharInTextNode(lastNode);
		}
	}
	
	function removeLastCharInTextNode(node) {
		var nodeVal = node.nodeValue;
		node.nodeValue = nodeVal.substr(0, nodeVal.length - 1);
	}
	
	$(document).ready(function() {
		$('#pages').delegate('.column .column-inner', 'click', onColumnClick);
		
		var column1 = document.getElementById('column1');
		var nodes = column1.childNodes;
		
		//		console.log(nodes);
		
		removeLastChar(nodes);
		
	//		$('.column-inner').each(function() {
	//			var text = $(this).text();
	//			var newText = '';
	//			
	//			for (var i = 0; i < text.length; i++) {
	//				newText += '<span class="letter">' + text.charAt(i) + '</span>';
	//			}
	//			
	//			$(this).html(newText);
	//		});
		
	//		$(document).keydown(function(e){
	//			
	//			
	//			//			console.log("Saved height: " + selectedTextBoxCurrentHeight);
	//			//			console.log("Current height: " + currentHeight);
	//
	//			currentHeight = $(selectedTextBox).height();
	//				
	//			while(currentHeight > selectedTextBoxCurrentHeight) {
	//				
	//				currentHeight = $(selectedTextBox).height();
	//			}
	//		});
	});
})(jQuery, propsage.ui);

$.fn.copyCSS = function (source) {
	var dom = $(source).get(0);
	var dest = {};
	var style, prop;
	if (window.getComputedStyle) {
		var camelize = function (a, b) {
			return b.toUpperCase();
		};
		if (style = window.getComputedStyle(dom, null)) {
			var camel, val;
			if (style.length) {
				for (var i = 0, l = style.length; i < l; i++) {
					prop = style[i];
					camel = prop.replace(/\-([a-z])/, camelize);
					val = style.getPropertyValue(prop);
					dest[camel] = val;
				}
			} else {
				for (prop in style) {
					camel = prop.replace(/\-([a-z])/, camelize);
					val = style.getPropertyValue(prop) || style[prop];
					dest[camel] = val;
				}
			}
			return this.css(dest);
		}
	}
	if (style = dom.currentStyle) {
		for (prop in style) {
			dest[prop] = style[prop];
		}
		return this.css(dest);
	}
	if (style = dom.style) {
		for (prop in style) {
			if (typeof style[prop] != 'function') {
				dest[prop] = style[prop];
			}
		}
	}
	return this.css(dest);
};